<?php

	# Class: FileBase
	# -------------------------------------------
	# Class to Manager File, including reading, uploading, downloading actions.

	
	class Joob_Utils_File{
	
		# Class Constructor		
		function __construct($file=''){
			$this->fh=null;
			$this->file=$file;
		}
		
		
		# Class Destructor		
		function __destruct(){ 
			$this->close();
		}
		
		
		# Function: select(&$file,&$folder='',&$mod='w')
		# ------------------------------------------
		# Select the file for manipulation. It is an alias of a private
		# function selectFile().
		
		public function select($file,$folder='',$mod='w'){
			return $this->selectFile($file,$folder,$mod);
		}
		
		
		# Function: write(&$data)
		# ------------------------------------------
		# Write a string into the file. 
		# It is an alias of a private function writeText().		
		
		public function write($data){
			return $this->writeText($data);
		}
		
		
		# Function: read()
		# ------------------------------------------
		# Read contents of the file. Called by the common method
		# get_file_contents().
		
		public function read(){
			return file_get_contents($this->file);
		}
		
		
		# Function: close()
		# --------------------------------------------
		# Simply close the file handler.
		
		private function close(){
			if ($this->fh!=null)
			{
				fclose($this->fh);
				$this->fh=null;
			}
		}
		
		
		# Function idle()
		# ------------------------------------------
		# Check if the current instance is idle or not - work by checking if
		# the fh is null or not.
		
		private function idle()
		{
			if ($this->fh==null) return true;
			return false;
		}
		
		
		# Function: activate()
		# ----------------------------------------------
		# Activate the file and ready for action.
		
		private function activate(){
			if ($this->fh==null){
				$this->fh = fopen($this->file, $this->mod) or die("Can't Open File");
			}
		}
		
		
		# Function: testFile()
		# --------------------------------------------
		# Test if the file handler is not null. Otherwise, it will
		# terminate the program.
		
		private function testFile(){
			confirm($this->fh!=null);
		}
		
		
		# Function: writeText($str)
		# ---------------------------------------------
		# Write $str to the file. This private function is called
		# by its alias public function write().
		
		private function writeText($data){
			$this->activate();
			$this->testFile();
			fwrite($this->fh, $data);
		}
		
		
		# Function: selectFile()
		# ---------------------------------------------
		# Select file for future manipulation. This private function is 
		# called by its alias public function select().
		
		private function selectFile($file,$folder='',$mod='w'){
			$file=$folder."/".$file;
			if ($file!=$this->file || $mod!=$this->mod){
				$this->file=$file;
				$this->mod=$mod;
				$this->close();
			}
		}
		

		# VARIABLES
		# -----------------------------------------------
		
		protected $file, $fh, $mod;
	}
?>